//
//  AppDelegate.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/7/27.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabbarController.h"
#import "QQLoginViewController.h"
#import "QQNavigationController.h"
#import "HGPlaceOderVC.h"
#import <AlipaySDK/AlipaySDK.h>
#import <UMShare/UMShare.h>
#import <UMCommon/UMCommon.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [NSThread sleepForTimeInterval:3];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    [self.window makeKeyAndVisible];
    
    [self themeConfiguration];
    
    if(!kQQUserInfoManger.isLogin) {
        //        HGPlaceOderVC* loginVC = [[HGPlaceOderVC alloc]init];
        QQLoginViewController* loginVC = [[QQLoginViewController alloc]init];
        QQNavigationController* navVC = [[QQNavigationController alloc]initWithRootViewController:loginVC];
        self.window.rootViewController = navVC;
    }else{
        self.window.rootViewController = [[MainTabbarController alloc] init];
    }
    
    [[SDWebImageManager sharedManager] setCacheKeyFilter: ^(NSURL *url) {
        if (!([[url absoluteString] hasPrefix:@"http://"] || [[url absoluteString] hasPrefix:@"https://"])) return @"";
        url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
        return [url absoluteString];
    }];
    
    [self cofginUMShare];
    [WXApi registerApp:@"wx8c0585d595d4ebb0"];
    
    return YES;
}

- (void)themeConfiguration {
    if (@available(iOS 11.0, *)){
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }
    // item 图片颜色
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
    // item 文字颜色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    [[XNProgressHUD shared] setMaximumDelayDismissDuration:MAXFLOAT];
    [[XNProgressHUD shared] setOrientation:(XNProgressHUDOrientationVertical)];
    [[XNProgressHUD shared] setTintColor:kColorRGBAlpha(0, 0, 0, 0.7)];
    [[XNProgressHUD shared] setDisposableDelayResponse:0.2 delayDismiss:2];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)cofginUMShare {
    
    [[UMSocialManager defaultManager] setUmSocialAppkey:@"598b04535312dd365f000031"];
    /* 打开调试日志 */
    [[UMSocialManager defaultManager] openLog:YES];
    /* 设置微信的appKey和appSecret */
    //AppID:wx8c0585d595d4ebb0
    //AppSecret:6057d447a493059818950aa13a3b4f43
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx8c0585d595d4ebb0" appSecret:@"6057d447a493059818950aa13a3b4f43" redirectURL:@"http://mobile.umeng.com/social"];
    /*
     * 移除相应平台的分享，如微信收藏
     */
    [[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
    /* 设置分享到QQ互联的appID
     * U-Share SDK为了兼容大部分平台命名，统一用appKey和appSecret进行参数设置，而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1106343194"/*设置QQ平台的appID*/  appSecret:@"XhiRbz2TfV1XUojc" redirectURL:@"http://mobile.umeng.com/social"];
    
    [UMConfigure initWithAppkey:@"5b34516ff29d98783900000f" channel:@"App Store"];
}


// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
    
    //    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    //    if (!result) {
    // 其他如支付等SDK的回调
    //    }
    if ([url.host isEqualToString:@"safepay"]) {
        // 支付跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            if ([resultDic[@"resultStatus"] integerValue] == 9000) {
                [[XNProgressHUD shared] showSuccessWithTitle:@"支付成功"];
                [[NSNotificationCenter defaultCenter] postNotificationName:kAliPayScuess object:nil userInfo:@{@"payNo" : resultDic[@"result"]}];
            } else if ([resultDic[@"resultStatus"] integerValue] == 6001) {
                [[XNProgressHUD shared] showErrorWithTitle:@"用户取消支付"];
            } else if ([resultDic[@"resultStatus"] integerValue] == 4000) {
                [[XNProgressHUD shared] showErrorWithTitle:@"订单支付失败"];
            }
        }];
        
    }
    //调用 支付结果的回调 (设置代理)
        [WXApi handleOpenURL:url delegate:self];
    //    return result;
    return YES;
}


-(void)onResp:(BaseResp*)resp{
    if ([resp isKindOfClass:[PayResp class]]){
        switch (resp.errCode) {
            case WXSuccess:
            {
                [[NSNotificationCenter defaultCenter] postNotificationName:kAliPayScuess object:nil userInfo:@{@"pay" : @"weixin"}];
            }
                break;
            case WXErrCodeCommon:
                [[XNProgressHUD shared] showErrorWithTitle:@"普通错误类型"];
                break;
            case WXErrCodeUserCancel:
                [[XNProgressHUD shared] showErrorWithTitle:@"微信支付-用户点击取消并返回"];
                break;
            case WXErrCodeSentFail:
                [[XNProgressHUD shared] showErrorWithTitle:@"发送失败"];
                break;
            case WXErrCodeAuthDeny:
                [[XNProgressHUD shared] showErrorWithTitle:@"授权失败"];
                break;
            case WXErrCodeUnsupport:
                [[XNProgressHUD shared] showErrorWithTitle:@"微信不支持"];
                break;
            default:
                break;
        }
    }
}


@end
